//
//  EmptyViewHelper.m
//  TooToo
//
//  Created by liuning on 15/10/27.
//  Copyright © 2015年 MoHao. All rights reserved.
//

#import "EmptyViewHelper.h"
#define EMPTY_TAG 0x5f3a
#define EMPTY_IMAGE_TAG (0x5f3a+1)
#define EMPTY_LABEL_TAG (0x5f3a+2)

@implementation EmptyViewHelper

+(UIView *)showEmptyOnView:(UIView *)view imageName:(NSString *)imgName text:(NSString *)text
{
    [self removeEmptyFromView:view];
    
    UIView * emptyView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)];
    emptyView.backgroundColor = [UIColor whiteColor];
    emptyView.tag = EMPTY_TAG;
    [view addSubview:emptyView];
    
    float width = 160;
    
    UIImageView * imgView;
    UILabel * label;
    CGPoint center = emptyView.center;
    
    if (imgName) {
        imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, width, width)];
        imgView.image = [UIImage imageNamed:imgName];
        imgView.contentMode = UIViewContentModeScaleAspectFit;
        imgView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |  UIViewAutoresizingFlexibleBottomMargin;
        
        imgView.tag = EMPTY_IMAGE_TAG;
        [emptyView addSubview:imgView];
    }
    
    if (text) {
        label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, view.bounds.size.width, 16)];
        label.font = [UIFont systemFontOfSize:15.0f];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = UIColorFromRGB(0xC6C5CC);
        label.backgroundColor = [UIColor clearColor];
        label.tag = EMPTY_LABEL_TAG;
        label.text = text;
        label.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |  UIViewAutoresizingFlexibleBottomMargin;
        [emptyView addSubview:label];
    }
    
    if (imgView && label) {
        imgView.center = CGPointMake(center.x, center.y - 35*UISCALE);
//        label.center = CGPointMake(center.x, imgView.frame.origin.y+imgView.frame.size.height+15+label.bounds.size.height/2.0f);
        
        [imgView addSubview:label];
        label.centerX = imgView.width/2.0f;
        label.top = imgView.height;
        
    }else{
        
        imgView.center = CGPointMake(center.x, center.y - 20);
        label.center = CGPointMake(center.x, center.y - 20);
    }
    
    UIView * navBar = [view viewWithTag:TOP_NAVBAR_TAG];
    if (navBar) {
        [view bringSubviewToFront:navBar];
    }
    
    return emptyView;
}
+(void)removeEmptyFromView:(UIView *)view
{
    UIView * emptyView = [view viewWithTag:EMPTY_TAG];
    if (emptyView) {
        [emptyView removeFromSuperview];
    }
}
+(UILabel *)getLabelFromEmptyView:(UIView *)emptyView
{
    return (UILabel *)[emptyView viewWithTag:EMPTY_LABEL_TAG];
}
+(UIImageView *)getImageViewFromEmptyView:(UIView *)emptyView
{
    return (UIImageView *)[emptyView viewWithTag:EMPTY_IMAGE_TAG];
}
@end
